<template>
  <div class="dashboard-container">
    <div class="app-container">
      <h2>组织架构</h2>
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :is-root="isRoot" />
        <el-tree :data="departs" :props="defaultProps">
          <tree-tools
            @editDepts="editDepts"
            @handleDialog="openDialog"
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartMents"
          />
        </el-tree>
      </el-card>
      <add-dept
        ref="addDept"
        :type-status="typeStatus"
        :show-dialog="showDialog"
        @closeDia="closeDialog"
        :tree-node="treeNode"
        @delDepts="getDepartMents"
      />
      <el-card>
        <el-input-number v-model="num" :min="1" :max="10" label="描述文字" @change="saveCountA" />
      </el-card>
      <el-card>
        <h3>省市区三级联动</h3>
        <v-distpicker />
      </el-card>
      <el-card>
        <el-cascader
          v-model="selectedOptions"
          size="large"
          :options="options"
          @change="handleChange"
        />

        <div style="height: 300px"></div>
      </el-card>
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue';
import { getDepartMents } from '@/api/departments';
import { tranListToTreeData } from '@/utils/index';
import { regionData } from 'element-china-area-data';
import { mapActions } from 'vuex';
import addDept from './components/add-dept.vue';
export default {
  name: 'Departments',
  components: {
    treeTools,
    addDept,
  },
  data() {
    return {
      typeStatus: '',
      treeNode: {},
      showDialog: false,
      num: 5,
      options: regionData,
      selectedOptions: [],
      departs: [],
      defaultProps: {
        label: 'name',
      },
      company: { name: 'kky责任国际有限公司', manager: 'kky' },
      isRoot: true,
    };
  },
  created() {
    this.getDepartMents();
    if (localStorage.count) {
      this.$store.dispatch('saveCountA', localStorage.count);
      this.num = localStorage.count;
    } else {
      this.$store.dispatch('saveCountA', this.num);
    }
  },
  mounted() {},
  methods: {
    async getDepartMents() {
      const result = await getDepartMents();
      this.departs = tranListToTreeData(result.data.data.depts, '');
    },
    ...mapActions(['saveCountA']),
    handleChange(value) {},
    closeDialog() {
      this.showDialog = false;
    },
    openDialog(node, type) {
      this.showDialog = true;
      this.treeNode = node;
      console.log('type:', type);
      this.typeStatus = type;
    },
    editDepts(node) {
      this.showDialog = true;
      this.treeNode = node;
      // 在父组件调用子组件的方法
      this.$refs.addDept.getDepartDetail(node.id);
    },
  },
};
</script>

<style lang="scss">
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
